﻿using AutoStar.Common;
using AutoStar.Models;
using AutoStar.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoStar.Views.Pages
{
    /// <summary>
    /// SettingView.xaml 的交互逻辑
    /// </summary>
    public partial class SettingView : Page
    {
        public SettingView()
        {
            InitializeComponent();
            DataContext = Global.SettingVM;
            Global.SettingVM.InitializeChannels();
            this.FontSize = 18;
            //Global.SettingVM.SerialPortNum = "COM6";
            ChannelsList.ItemsSource = Global.SettingVM.channels;
            //// 设置 ChannelsList 的数据源
            //ChannelsList.ItemsSource = Global.SettingVM.Channels;
            //// 设置下拉 SelectedCategory 的数据源
            //Global.SettingVM.Categories = Global.SettingVM.ChannelTypes;
        }

        private void SelectImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image files (*.png;*.jpeg;*.jpg)|*.png;*.jpeg;*.jpg|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Configuration config = Global.config;
                if (config.AppSettings.Settings["LogoPath"] != null)
                    config.AppSettings.Settings["LogoPath"].Value = openFileDialog.FileName;
                else
                    config.AppSettings.Settings.Add("LogoPath", openFileDialog.FileName);
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");

                Global.MainVM.LogoPath = openFileDialog.FileName;
                CurrentLogoPath.Text = "当前Logo路径：" + Global.MainVM.LogoPath;
            }
        }

        private void SaveSetting_Click(object sender, RoutedEventArgs e)
        {
            Global.SettingVM.SaveChannels();
            Global.SettingVM.ControlEnabled = false;
        }

        private void EditSetting_Click(object sender, RoutedEventArgs e)
        {
            Global.SettingVM.ControlEnabled = true;
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.FontSize = e.NewSize.Width / 70;
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            Global.SettingVM.SerialPorts = new ObservableCollection<string>();
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                Global.SettingVM.SerialPorts.Add(port);
            }
            if (Global.SettingVM.SerialPorts.Count > 0)
                Global.SettingVM.SelectedSerialPorts = Global.SettingVM.SerialPorts[0];
            else
                Global.SettingVM.SelectedSerialPorts = "";
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            Global.SettingVM.RefreshSerialPorts_Execute();
        }
    }
}
